const util = require('../../utils/timeTool.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    red:true,
    id:'',
    inputname:'',
    focus:false,
    isOpen:false,
    isOpen2:false,
    isReturn:false,
    birthdayPerson:'',
    birthId:'',
    relationId:'',
    birthday:'',
    birthdayLunarStr: '',
    birthdayDefault:'',
    blessings:'',
    redBlessings:'',
    posterUrl:'',
    list:[],
    userInfo:'',
    sendName:'',
    redId:'',
    returninput:'',
    redPacket:'',
    sendUrl:'',
    wholeopen: false,
    nrheight:0,
    urls:'',
    isShare: 0,
    yearisClick:'',
    scrollBtn:false,
    helfH:'',
    textH:'',
    createUid:'',
    isMyred:false,
    sendTime:'',
    receiveName:'',
    receiveUrl:'',
    isopens:'',
    island: false,
    loginShow: false,
  },
  //点击送祝福
  topLeaveMsg: function () {
    if (!this.data.island) {
      this.landbtn()
    } else {
      this.setData({
        focus: true
      })
    }
  },
  //阅读全部
  wholeopen() {
    this.setData({
      wholeopen: !this.data.wholeopen,
    })
  },
  //送祝福
  formName: function (e) {
    this.setData({
      inputname: e.detail.value.replace(/\s+/g, '')
    })
    console.log('红包', this.data.inputname)
  },
  bindblur: function (e) {
    this.setData({
      inputname: e.detail.value.replace(/\s+/g, ''),
      focus: false
    })
  },
  //感谢
  returninput(e){
    this.setData({
      returninput: e.detail.value
    })
  },
  clickMyred(e){
    var that= this
    console.log(e)
    var redid = e.currentTarget.dataset.redid
    app.req({
      url: "wechat/birthManagePart/info",
      method: "post",
      data: {
        id: redid,
        birthId: that.data.id
      },
      success: function (res) {
        if (200 == res.code) {
          console.log("红包", res)
          that.setData({
            redBlessings: res.data.blessings,
            sendName: res.data.sendName,
            redPacket: res.data.redPacket,
            sendUrl: res.data.sendUrl,
            receiveUrl: res.data.receiveUrl,
            receiveName: res.data.receiveName,
            sendTime: res.data.sendTime.substring(11, 16),
            isopens:res.data.isOpen,
          })
        }
      }
    })
    that.setData({
      isMyred: true,
    })
  },
  xClose1(){
    this.setData({
      isMyred: false,
    })
  },
  //确认  
  confirmreply: function (e) {
    let that = this
    console.log('发送',that.data.inputname)
    if (that.data.inputname == '') {
      wx.showToast({
        title: '内容不能为空',
        icon: 'none',
        duration: 2000
      })
    } else {
      app.req({
        url: "wechat/wxpay/sendRedEnvelope",
        method: "post",
        data: {
          birthId:that.data.id,
          blessingType:1,
          blessings: that.data.inputname,
          sendId: that.data.userInfo.id
        },
        success: function (res) {
          console.log(res)
          if(res.code==200){
            wx.showToast({
              title: '祝福成功',
              icon: 'none',
              duration: 2000
            })
            that.setData({
              inputname: ''
            })
            that.getDetails()
          }else{
            wx.showToast({
              title: '发送失败',
              icon: 'none',
              duration: 2000
            })
          }
        }
      });
    }
    this.setData({
      focus: false
    })
  },
  scrollBtn(){
    var that = this
    that.setData({
      scrollBtn: !that.data.scrollBtn
    })
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var helfH = clientHeight * 0.8 * rpxR;
        that.setData({
          helfH: helfH
        });
      }
    });
    that.getDetails()
  },
  lower(e) {
    this.getDetails()
  },
  gohome(){
    wx.switchTab({
      url: '/pages/index/index'
    })
  },
  //详情
  getDetails() {
    var that = this
    app.req({
      url: "wechat/birthManage/blessingDetail",
      method: "post",
      data: {
        id: that.data.id
      },
      success: function (res) {
        if (200 == res.code) {
          console.log("生日", res)
          var birthdayTime = res.data.birthday
          for (var item in res.data.list) {
            res.data.list[item].yearisClick = 'd'+item;
          }
          var yearisClick = 'd' + (res.data.list.length-1)
          that.setData({
            birthday: util.itemDateToShow(res.data.birthday, 1, false, false).substring(5, 11),
            birthdayLunarStr: res.data.birthdayLunarStr.substring(9, 13),
            birthdayDefault: res.data.birthdayDefault,
            birthdayPerson: res.data.birthdayPerson,
            list:res.data.list,
            blessings: res.data.blessings,
            birthId: res.data.id,
            relationId: res.data.relationId,
            urls:res.data.urls,
            yearisClick: yearisClick,
            createUid: res.data.createUid
          })
          console.log('list', that.data.list, that.data.yearisClick)
          //获取内容高度
          if (that.data.blessings || that.data.urls.length > 0) {
            var query = wx.createSelectorQuery();
            //选择id
            query.select('#mjltest').boundingClientRect()
            query.exec(function (res) {
              //res就是 所有标签为mjltest的元素的信息 的数组
              // console.log('高度信息', res);
              //取高度
              // console.log('高度', res[0].height);
              if (res[0].height > 110) {
                that.setData({
                  nrheight: 220
                })
              }
            })
          }
          //页面高度
          wx.getSystemInfo({
            success: function (res) {
              var clientHeight = res.windowHeight,
                clientWidth = res.windowWidth,
                rpxR = 750 / clientWidth;
              if (that.data.blessings || that.data.urls.length > 0){
                  var textH = clientHeight * rpxR - 1000;
                  // console.log('祝福高度1', that.data.textH)
                }else{
                  var textH = clientHeight * rpxR - 680;
                  // console.log('祝福高度2', that.data.textH)
                }
              that.setData({
                textH: textH
              });
              // console.log('祝福高度',that.data.textH)
            }
          });
        }
      }
    })
  },
  getBgImgList() {
    var that = this;
    app.req({
      url: 'wechat/image/list',
      data: {
        "type": 12
      },
      method: 'POST',
      success: function (res) {
        if (res.code == 200) {
          console.log('背景图', res)
          for (var i = 0; i < res.data.length; i++) {
            if (res.data[i].sort == 1) {
              that.setData({
                posterUrl: res.data[i].url,
              })
            }
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  //开红包
  redOpen(e){
    var that = this
    var id = e.currentTarget.dataset.redid
    var sendName = e.currentTarget.dataset.name
    console.log('hhhh', e)
    that.setData({
      redId: id
    })
    if(that.data.red){
      that.setData({
        red: false
      })
    if(that.data.userInfo.id != that.data.relationId){
      that.setData({
        isOpen: !this.data.isOpen,
        isOpen2: true,
      })
      app.req({
        url: "wechat/birthManagePart/info",
        method: "post",
        data: {
          id: that.data.redId,
          birthId: that.data.id
        },
        success: function (res) {
          if (200 == res.code) {
            console.log("红包", res)
            that.setData({
              redBlessings: res.data.blessings,
              sendName: sendName,
              redPacket: res.data.redPacket,
              sendUrl: res.data.sendUrl,
              red: true
            })
            }
        }
      })
    } else{
    app.req({
      url: "wechat/birthManagePart/viewRedEnvelope",
      method: "post",
      data: {
        id: that.data.redId,
        birthId: that.data.id
      },
      success: function (res) {
        if (200 == res.code) {
          console.log("看红包", res)
        }
      }
    })
    app.req({
      url: "wechat/birthManagePart/info",
      method: "post",
      data: {
        id: that.data.redId,
        birthId: that.data.id
      },
      success: function (res) {
        if (200 == res.code) {
          console.log("红包", res)
          that.setData({
            redBlessings: res.data.blessings,
            sendName: sendName,
            redPacket: res.data.redPacket,
            sendUrl: res.data.sendUrl,
            red: true
          })
          if (res.data.isOpen == 1 || res.data.isOpen == 2 || res.data.isOpen==4){
            wx.navigateTo({
              url: '/pages/DetailsRedPackets/detailsRedPackets?redId=' + that.data.redId
            });
          }else if( res.data.back){
            return
          } else{
            that.setData({
              isOpen: !this.data.isOpen
            })
          }
        }else{
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
    }
    }
  },

  openRed() {
    var that = this
    if(that.data.red){
      that.setData({
        red:false
      })
    app.req({
      url: "wechat/birthManagePart/receiveRedEnvelope",
      method: "post",
      data: {
        id: that.data.redId,
        birthId:that.data.id
      },
      success: function (res) {
        if (200 == res.code) {
          that.setData({
            isOpen: false,
            red:true
          })
          wx.navigateTo({
            url: '/pages/DetailsRedPackets/detailsRedPackets?redId=' + that.data.redId
          });
        }else{
          wx.showToast({
            title: res.msg,
            icon:'none',
            duration: 2000
          })
        }
      }
    })
    }
  },
  xClose(){
    this.setData({
      isOpen: !this.data.isOpen,
      isOpen2:false,
    })
  },
  //退款
  returnRed(){
    var that = this
    if (that.data.returninput==''){
      that.setData({
        returninput:'谢谢您的好意'
      })
    }
    app.req({
      url: "wechat/birthManagePart/retreatRedEnvelope",
      method: "post",
      data: {
        id: that.data.redId,
        birthId: that.data.id,
        blessings: that.data.returninput,
      },
      success: function (res) {
        if (200 == res.code) {
          wx.showToast({
            title: "红包已退回",
            duration: 2000
          })
          that.setData({
            isReturn: !that.data.isReturn
          })
        }else{
          wx.showToast({
            title: res.msg,
            icon:'none',
            duration: 2000
          })
        }
      }
    })
  },
  returnBtn() {
    var that = this
          that.setData({
            isReturn: !this.data.isReturn,
            isOpen: !this.data.isOpen
          })
  },
  xClosetwo:function(e) {
    console.log('点击')
    this.setData({
      isReturn: !this.data.isReturn
    })
  },


// 转跳送红包
  redEnvelopes:function(e) {
    if (!this.data.island) {
      this.landbtn()
    }else {
    console.log("点击送红包")
    wx.navigateTo({
      url: '/pages/RedEnvelopes/redEnvelopes?birthdayPerson=' + this.data.birthdayPerson + '&birthId=' + this.data.birthId + '&relationId=' + this.data.relationId
    })
    }
  },
  //相册
  albumimgEnd() {
    if (!this.data.island) {
      this.landbtn()
    } else {
      wx.navigateTo({
        url: '/pages/album/album?id=' + this.data.id + '&status=1' + '&originatorID=' + this.data.createUid
      })
    }
  },
//分享页面
  sharepage(){
    if (!this.data.island) {
      this.landbtn()
    } else {
    wx.navigateTo({
      url: '/pages/partyTitleTwo/partyTitleTwo?id='+this.data.id
    })
    }
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    if(options.isShare){
      that.setData({
        isShare: options.isShare
      })
    }
    that.setData({
      id: options.id || options.scene
    })
    console.log("活动id",options.id,options)
    console.log("isShare========")
    if (that.data.isShare == 1) {
      console.log("isShare====1")
      wx.setStorageSync('loginActId', options.id)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this
    var phone = wx.getStorageSync('phone');
    wx.getSetting({
      success: (res) => {
        console.log('授权', res)
        if (wx.getStorageSync('uid')) {
          console.log('已授权登陆')
          that.setData({
            island: true
          })
          //获取头像及名称
          app.getUserInfo(function (userInfo) {
            console.log("我的id", userInfo.id)
            that.setData({
              userInfo: userInfo
            })
          })
        }else{
          that.setData({
            island: false
          })
        }
      }
    })
   
    that.getDetails()
    that.getBgImgList()
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getDetails()
  },
  // 登录
  landbtn() {
    this.toggle('loginShow', true)
    this.selectComponent('#login').showClose()
  },
  toggleToast(e) {
    console.log(e.detail)
    if (!e.detail.loginShow){
      this.toggle('loginShow', false)
    }
    this.onShow()
  },
})